<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use app\common\tool\Result;
use think\facade\View;
use think\Response;
use think\response\Json;

class Product extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Product';

    //关联模型
    protected array $hasOne = ['cate1', 'cate2'];

    //验证器
    protected string $validate = 'Product';

    //自定义方法排序
    protected array $sort = ['id' => 'desc'];

    public array $tape = [
        ['field' => 'icon', 'title' => '图片', 'type' => 'icon'],
        ['field' => 'price', 'title' => '市场价'],
        ['field' => 'vip_price', 'title' => '会员价'],
        ['field' => 'stock', 'title' => '库存', 'type' => 'stock']
    ];

    public function index(): string
    {
        return Builder::Table()
            ->addColumn(['fixed' => 'left', 'title' => '全选', 'type' => 'checkbox'])
            ->addColumn(['field' => 'id', 'title' => 'ID', 'sort' => true, 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '商品名称', 'modify' => '2,input'])
            ->addColumn(['field' => 'cate_1', 'title' => '一级分类', 'modify' => '2,input', 'hide' => true])
            ->addColumn(['field' => 'cate_2', 'title' => '二级分类', 'modify' => '2,input', 'hide' => true])
            ->addColumn(['field' => 'cate', 'title' => '商品分类', 'templet' => '{{d.cate1?d.cate1.title:""}}-{{d.cate2?d.cate2.title:""}}'])
            ->addColumn(['field' => 'icon', 'title' => '商品主图', 'templet' => '#iconTpl'])
            ->addColumn(['field' => 'price', 'title' => '市场价', 'modify' => '2,number'])
            ->addColumn(['field' => 'vip_price', 'title' => '会员价', 'modify' => '2,number'])
            ->addColumn(['field' => 'stock', 'title' => '库存', 'modify' => '2,number'])
            ->addColumn(['field' => 'sales', 'title' => '销量', 'modify' => '2,number'])
            ->addColumn(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120])
            ->addColumn(['field' => 'pick', 'title' => '是否推荐', 'templet' => '#pick', 'width' => 120])
            ->addColumn(['field' => 'sort', 'title' => '排序', 'modify' => '2,number', 'templet' => '#sort_sort', 'width' => 120])
            ->addColumn(['field' => 'content', 'title' => '商品详情', 'templet' => '#editorTpl'])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right', 'title' => '操作', 'align' => 'center', 'toolbar' => '#barDemo', 'width' => 150])
            ->addSwitchTool([
                ['name' => 'status', 'pk' => $this->pk, 'text' => '上架|下架', 'val' => '1|0', 'checked' => 1],
                ['name' => 'pick', 'pk' => $this->pk, 'text' => '是|否', 'val' => '1|0', 'checked' => 1]
            ])
            ->addSortTool([['name' => 'sort', 'pk' => $this->pk, 'url' => url('update')->build()]])
            ->addTopButtons(['add', 'deleteAll'])
            ->addRightButtons(['edit', 'delete'])
            ->addSearch([
                ['title', '商品名称', 'input,Like'],
                ['cate_1,cate_2', '一级分类,二级分类', 'linkage', url('productCate/getCate')->build()]
            ])
            ->fetch();
    }

    public static function getCate(): array
    {
        return \app\common\model\ProductCate::where('pid', 0)->field('id,title')->with('child')
            ->order('sort desc, id asc')->select()->toArray();
    }

    public function add($tpl = 'product/edit'): string
    {
        View::assign([
            'cate' => self::getCate(),
            'tape' => $this->tape
        ]);

        return parent::add($tpl);
    }

    public function edit($id = '', $tpl = 'product/edit', $info = ''): string
    {
        View::assign([
            'cate' => self::getCate(),
            'tape' => $this->tape
        ]);

        return parent::edit($id, $tpl, $info);
    }

    public function save($param = [], $model = true): Json
    {
        $param = $this->request->post();
        $param['tape'] = $this->tape;

        return parent::save($param, $model);
    }

    public function delete(): Response
    {
        $param = $this->request->post();

        return Result::fastReturn($this->model::productDelete($param, $this->getLogsArguments($param)));
    }
}